using System;
using System.Collections.Generic;

using OpenMetaverse;
using Nini;
using Nini.Config;

namespace Playtime.RegionModule
{
	// This class describes a generic attribute
	public class Attribute
	{
		/// <summary>
		/// List of profiles where this attribute is referenced
		/// </summary>
		public List<Profile> Profiles { get; private set; }
		
		public static readonly UUID IntegerTypeID = new UUID("d6264c35-a20b-4a4a-9a97-a54e4573580f");
		public static readonly UUID FloatTypeID = new UUID("bac86791-48d1-499e-bb5a-3b5ac3251551");
		public static readonly UUID BoolTypeID = new UUID("bf616a22-7fbc-419d-a166-6d39d7193e31");
		public static readonly UUID StringTypeID = new UUID("49704a4b-ffe6-4456-9175-cb0da7bc0bcf");
		
		/// <summary>
		/// This defines the default Lua object name (not class name)
		/// </summary>
		public string DefaultObjectName { get { return Name; } }

		/// <summary>
		/// Globally unique identifier of this attribute
		/// </summary>
		public UUID ID;
		
		/// <summary>
		/// Userfriendly (short) name of this value, must be a valid Lua variable name
		/// </summary>
		public string Name;
		
		/// <summary>
		/// Type, described as a globally unique identifier.
		/// </summary>
		public UUID Type;
		
		public IAttributeInstance CreateInstance()
		{
			if(Type == IntegerTypeID)
				return new IntegerAttributeInstance(this);
			else if(Type == BoolTypeID)
				return new BoolAttributeInstance(this);
			else if(Type == FloatTypeID)
				return new FloatAttributeInstance(this);
			else if(Type == StringTypeID)
				return new StringAttributeInstance(this);
			else
				return new AttributeInstance(this);
		}
		
		public Attribute()
		{
			Profiles = new List<Profile>();
		}
		
		public Attribute(IConfig attributeDescription) : this()
		{
			if(attributeDescription == null)
				throw new PlaytimeException("missing attribute description");

			this.ID = new UUID(attributeDescription.Get("ID"));
			this.Name = attributeDescription.Get("Name");
			string typeDescription = attributeDescription.Get("Type");
			if(typeDescription.ToLower() == "integer")
				this.Type = Attribute.IntegerTypeID;
			else if(typeDescription.ToLower() == "bool" || typeDescription.ToLower() == "boolean")
				this.Type = Attribute.BoolTypeID;
			else if(typeDescription.ToLower() == "float")
				this.Type = Attribute.FloatTypeID;
			else if(typeDescription.ToLower() == "string")
				this.Type = Attribute.StringTypeID;
			else
				this.Type = new UUID(typeDescription);
		}

	}
}

